/************************************************************************************
* Utils.cpp                                                                         *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "Utils.h"
#include "Constants.h"

#include <QTime>
#include <QDebug>

Utils::Utils() {

}


bool Utils::isSupportedFileExtention(QString ext) {
    for(int i = 0; i < Constants::NumSupportedFormats; i++) {
        if(ext == Constants::FileExtentions[i]) {
            return true;
        }
    }
    return false;
}


bool Utils::isSupportedFile(QFileInfo file) {
    return isSupportedFileExtention(file.suffix());
}


QString Utils::getStringTime(int timeInSeconds) {
        int hours = timeInSeconds / (60 * 60);
        int min   = timeInSeconds / 60;
        int sec   = timeInSeconds % 60;
        QTime displayTime(hours , min, sec);
        return (hours == 0? displayTime.toString("mm:ss") :
                            displayTime.toString("hh:mm:ss"));
}



QImage* Utils::imageForTag(TagLib::ID3v2::Tag *tag)
{
    TagLib::ID3v2::FrameList l = tag->frameList("APIC");
    if(l.isEmpty()) {
        return new QImage(":/no_cover");
    }
    QImage* image = new QImage();
    TagLib::ID3v2::AttachedPictureFrame *f =
        static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
    image->loadFromData((const uchar *) f->picture().data(), f->picture().size());

    return image;
}

QImage* Utils::fetchCover(QString file)
{
    TagLib::MPEG::File f(file.toUtf8());
    if(!f.isValid() || !f.ID3v2Tag()) {
        return new QImage(":/no_cover");
    }
    return imageForTag(f.ID3v2Tag());
}


MediaRating Utils::getMediaRating(int rating) {
    switch(rating) {
    case 0: return None;
    case 1: return Bad;
    case 2: return Average;
    case 3: return Good;
    case 4: return VeryGood;
    case 5: return Excellent;
    }
}


int Utils::getIntegerFromRating(MediaRating rating) {
    switch(rating) {
    case None     : return 0;
    case Bad      : return 1;
    case Average  : return 2;
    case Good     : return 3;
    case VeryGood : return 4;
    case Excellent: return 5;
    }
}
